<?php
$page_name=basename($_SERVER['SCRIPT_FILENAME']);
$IsPreserved	= 'Y';
$IsProcess		= $_REQUEST['IsProcess'];
include("general_include.php");
include "checklogin.php";

if($IsProcess <> 'Y')
{
	include "top.php";
}	

$action	= $_REQUEST['action'];

if($action=="del")
{	
	if(!empty($delete_id))
	{
	
	  $DelSql = "DELETE FROM ".TABLEPREFIX."_lesson where lesson_id='$delete_id'";
	  $UserManagerObjAjax->Execute($DelSql);
	  
    }	  	 
}  

if($action=="complete"){

	$id= $_REQUEST['lesson_id'];
	
	$UpdateLessonSql="UPDATE ".TABLEPREFIX."_lesson SET lesson_status_id=3,date_edited='".date('Y-m-d H:i:s')."' WHERE lesson_id=".$id;
	$UserManagerObjAjax->Execute($UpdateLessonSql);
}

if($action=="list_search")
{ 
		$Search_lesson_title = $Search_lesson_title=='Search by title'?'':$Search_lesson_title;
		$Search_coach_id = $Search_coach_id;
		$Search_lesson_status = $Search_lesson_status;
		$Search_from_date = $Search_from_date=='From'?'':$Search_from_date;
		$Search_to_date = $Search_to_date=='To'?'':$Search_to_date;
		
		if(!empty($Search_lesson_title))
		{
			 $searchSql =  $searchSql." AND lesson_title like '%".trim(mysql_quote($Search_lesson_title,"N"))."%'"; 
		}
		
		if(!empty($Search_coach_id))
		{
			 $searchSql =  $searchSql." AND coach_id=".$Search_coach_id; 
		}
		
		if(!empty($Search_lesson_status))
		{
			 $searchSql =  $searchSql." AND lesson_status_id=".$Search_lesson_status; 
		}
		
		if(!empty($Search_from_date))
		{
			 $fromDate = $Search_from_date.' 00:00:00';
			 $searchSql =  $searchSql." AND date_added >= '".mysql_quote($fromDate,"N")."'"; 
		}
		
		if(!empty($Search_to_date))
		{
			 $toDate = $Search_from_date.' 23:59:59';
			 $searchSql =  $searchSql." AND date_added <= '".mysql_quote($toDate,"N")."'"; 
		}
		
		$SearchLink="dosearch=GO&Search_language=$Search_language";
}	

$CoachgameSql = "SELECT * FROM ".TABLEPREFIX."_lesson WHERE student_id=".$_SESSION['user_id'].$searchSql." ORDER BY date_added DESC";
$CoachgameArr = $UserManagerObjAjax->GetRecords("All",$CoachgameSql);

$Numcoachgame=count($CoachgameArr);

for($g=0;$g<$Numcoachgame;$g++)
{
	$CoachgameArr[$g]['date_added'] = $CoachgameArr[$g]['date_added'];
	$CoachgameArr[$g]['lesson_id'] = $CoachgameArr[$g]['lesson_id'];
	$CoachgameArr[$g]['lesson_title'] = $CoachgameArr[$g]['lesson_title'];
	$CoachgameArr[$g]['coach_id'] = $CoachgameArr[$g]['coach_id'];
	
	$Sql_Name="SELECT name FROM ".TABLEPREFIX."_user WHERE user_id=".$CoachgameArr[$g]['coach_id'];
	$Sql_NameArr = $UserManagerObjAjax->GetRecords("Row",$Sql_Name);
	
	$CoachgameArr[$g]['coach_name'] = $Sql_NameArr['name'];
	$CoachgameArr[$g]['lesson_price'] = number_format($CoachgameArr[$g]['lesson_price'], 2, '.', '');
	$CoachgameArr[$g]['lesson_status_id'] = $CoachgameArr[$g]['lesson_status_id'];
	
	$Sql_Status="SELECT lesson_status_title FROM ".TABLEPREFIX."_lesson_status WHERE lesson_status_id=".$CoachgameArr[$g]['lesson_status_id'];
	$Sql_StatusArr = $UserManagerObjAjax->GetRecords("Row",$Sql_Status);
	
	$CoachgameArr[$g]['lesson_status'] = $Sql_StatusArr['lesson_status_title'];
}

/* Coach Record */
/*echo $CoachRecSql="SELECT us.name,les.coach_id FROM ".TABLEPREFIX."_lesson AS les,".TABLEPREFIX."_user AS us WHERE les.coach_id=us.user_id and les.student_id=".$_SESSION['user_id']." GROUP BY us.name";
$CoachRecArr=$UserManagerObjAjax->GetRecords("All",$CoachRecSql);
for($i=0;$i<count($CoachRecArr);$i++)
{
$SelectCoachArr[$i]['coach_id'] = $CoachRecArr[$i]['coach_id'];

$SelectCoachArr[$i]['coach_name'] = $CoachRecArr[$i]['name'];
}	*/

$CoachRecSql="SELECT us.name,les.coach_id FROM ".TABLEPREFIX."_lesson AS les,".TABLEPREFIX."_user AS us WHERE les.coach_id=us.user_id and les.student_id=".$_SESSION['user_id']." GROUP BY us.name";
$CoachRecArr=$UserManagerObjAjax->GetRecords("All",$CoachRecSql);
for($i=0;$i<count($CoachRecArr);$i++)
{
$SelectCoachArr[$i]['coach_id'] = $CoachRecArr[$i]['coach_id'];

$CoachSql="SELECT name FROM ".TABLEPREFIX."_user WHERE user_id=".$SelectCoachArr[$i]['coach_id'];
$CoachName = $UserManagerObjAjax->GetRecords("Row",$CoachSql);

$SelectCoachArr[$i]['coach_name'] 	= $CoachName['name'];
}	

/* Coach Record */

/* Status Record */
$StatusRecSql="SELECT lesson_status_id,lesson_status_title FROM ".TABLEPREFIX."_lesson_status ";
$StatusRecArr=$UserManagerObjAjax->GetRecords("All",$StatusRecSql);
for($i=0;$i<count($StatusRecArr);$i++)
{
$SelectStatusArr[$i]['lesson_status_id'] = $StatusRecArr[$i]['lesson_status_id'];
$SelectStatusArr[$i]['lesson_status_title']	= $StatusRecArr[$i]['lesson_status_title'];
}	
/* Status Record */
$smarty->assign('is_coach',$is_coach);
$smarty->assign('is_partner',$is_partner);
$smarty->assign('Numcoachgame',$Numcoachgame);
$smarty->assign('CoachgameArr',$CoachgameArr);
$smarty->assign('SelectStatusArr',$SelectStatusArr);
$smarty->assign('SelectCoachArr',$SelectCoachArr);
$smarty->assign('Search_lesson_title',$Search_lesson_title);
$smarty->assign('Search_lesson_status',$Search_lesson_status);
$smarty->assign('Search_from_date',$Search_from_date);
$smarty->assign('Search_to_date',$Search_to_date);
$smarty->assign('Search_coach_id',$Search_coach_id);
$smarty->display('my_lesson.tpl');
if($IsProcess <> 'Y')
{
	include "footer.php";
}
?>